package com.usian.behavior.controller.v1;

import com.usian.behavior.entity.ApLikesBehavior;
import com.usian.behavior.entity.ApUnlikesBehavior;
import com.usian.behavior.service.IApLikesBehaviorService;
import com.usian.behavior.service.IApUnlikesBehaviorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

// 用户点赞以及不喜欢查询
@RestController
@RequestMapping("/api/v1/behavior")
public class ApBehaviorController {

    @Autowired
    private IApLikesBehaviorService apLikesBehaviorService;

    @Autowired
    private IApUnlikesBehaviorService apUnlikesBehaviorService;

    /**
     * 根据用户id 设备号 文章id查询 点赞行为、不喜欢行为
     * @param userId
     * @param equipmentId
     * @param articleId
     * @param likeType
     * @return
     */
    @GetMapping("/findBehavior")
    public Map<String,Boolean> findBehavior(@RequestParam(name = "userId") Integer userId,
                                            @RequestParam(name = "equipmentId") String equipmentId,
                                            @RequestParam(name = "articleId") Long articleId,
                                            @RequestParam(name = "type") Integer likeType){
        Boolean isLike=false;//是否点赞
        Boolean isUnlike=false;//是否不喜欢
        //查询文章是否被点赞
        ApLikesBehavior apLikesBehavior = apLikesBehaviorService.findLikeByArticleId(userId,equipmentId,articleId,likeType);
        if (apLikesBehavior!=null && apLikesBehavior.getOperation()==1){
            //点赞了
            isLike=true;
        }
        //查询文章是否被不喜欢
        ApUnlikesBehavior apUnlikesBehavior = apUnlikesBehaviorService.findUnLikeByArticleId(userId,equipmentId,articleId);
        if (apUnlikesBehavior!=null && apUnlikesBehavior.getType()==1){
            //不喜欢
            isUnlike=true;
        }
        //封装数据
        Map<String,Boolean> map = new HashMap<>();
        map.put("islike",isLike);
        map.put("isunlike",isUnlike);
        return map;
    }
}

